package util.common;

import java.util.Arrays;
import java.util.List;


public class StringUtils {
    /**
     * 检测字符串是否只包含空白字符
     */
    public static boolean isNullOrWhiteSpace(String str){
        if (str == null || str.length() == 0)
            return true;
        char[] whiteChars = {'\t', '\n', '\r', ' '}; // 从小到达
        char[] chars = str.toCharArray();
        for (char c : chars){
            if (Arrays.binarySearch(whiteChars, 0, whiteChars.length, c) < 0)
                return false;
        }
        return true;
    }

    /**
     * 检测字符串不是只包含空白字符
     */
    public static boolean isNotNullOrWhiteSpace(String str){
        return !isNullOrWhiteSpace(str);
    }

    /**
     * 判断两个字符串是否相同
     * @param str1
     * @param str2
     * @return
     */
    public static boolean isEquals(String str1, String str2){
        if (str1 == null && str2 == null){
            return true;
        }else if (str1 == null ||  str2 == null){
            return false;
        }else if (str1.length() != str2.length()){
            return false;
        }

        char[] c1 = str1.toCharArray();
        char[] c2 = str2.toCharArray();
        for (int i = 0; i < c1.length; i++) {
            if (c1[i] != c2[i])
                return false;
        }
        return true;
    }

    /**
     * 合并集合
     * @param separator
     * @param list
     * @return
     */
    public static String join(String separator, List list){
        StringBuilder builder = new StringBuilder();
        for (Object o: list){
            builder.append(separator).append(o.toString());
        }
        return builder.delete(0, separator.length()).toString();
    }

    /**
     * 合并集合
     * @param separator
     * @param objects
     * @return
     */
    public static String join(String separator, Object ... objects){
        StringBuilder builder = new StringBuilder();
        for (Object o: objects){
            builder.append(separator).append(o.toString());
        }
        return builder.delete(0, separator.length()).toString();
    }

    public static String[] splitRemoveEmpty(String str, String regex){
        String[] strs = str.split(regex);
        int i, j;
        for (i = j = 0; i < strs.length; i++) {
            if (StringUtils.isNotNullOrWhiteSpace(strs[i])){
                strs[j++] = strs[i];
            }
        }
        return i == j ? strs : Arrays.copyOf(strs, j);
    }

    public static int toInteger(String str){
        if (str == null)
            throw new IllegalArgumentException("str不能为空。");
        try {
            return Integer.valueOf(str);
        }catch (NumberFormatException e){
            return -1;
        }
    }

    /**
     * 传入数字字符串数组，转化为数字数组
     * @param str
     * @param operator 分隔符
     * @return
     */
    public static Integer[] splitIntsString(String str, String operator) {
        String[] arr = splitRemoveEmpty(str, operator);
        Integer[] arr2 = new Integer[arr.length];
        try {
            for (int i = 0; i < arr.length; i++) {
                arr2[i] = Integer.valueOf(arr[i].trim());
            }
            return arr2;
        }catch (NumberFormatException e){
            throw new IllegalArgumentException("无法分割成数字数组：" + str);
        }
    }

    public static String cutString(String description, int length) {
        StringBuilder builder = new StringBuilder(description);
        if (builder.length() > length){
            return builder.replace(length-6, length, "......").substring(0,length);
        }
        return builder.toString();
    }
}
